﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Countdown
{
    public partial class Window1
    {
        private Countdowner _counter;
        private Color _currentColor;

        public Window1()
        {
            InitializeComponent();
        }

        private void btnGo_Click( object sender, RoutedEventArgs e )
        {
            _currentColor = Colors.LightBlue;
            Start();
        }

        private void Start()
        {
            if ( _counter != null )
                _counter.Stop();
            _counter = new Countdowner( 12.Minutes(),
                                       ( timeLeft ) => UpdateDisplay( timeLeft ),
                                       () => txtStatus.DataContext = "You're done!",
                                       new Dictionary<TimeSpan, Action<TimeSpan>>()
                                           {
                                               {10.Minutes(), timeLeft =>
                                                                  {
                                                                      txtStatus.DataContext = "Better get started!";
                                                                     _currentColor = Colors.LightGreen;
                                                                  }},
                                               {5.Minutes(), timeLeft =>
                                                                 {
                                                                     txtStatus.DataContext =
                                                                         "Time for questions... keep going!";
                                                                     _currentColor = Colors.Orange;
                                                                 }},
                                               {30.Seconds(), timeLeft =>
                                                                  {
                                                                      txtStatus.DataContext =
                                                                          "Getting low on time!  Be careful!";
                                                                      _currentColor = Colors.OrangeRed;
                                                                      var red = new SolidColorBrush(Colors.Red);
                                                                      var anima = new ColorAnimation(Colors.Red,
                                                                                                     Colors.Orange,
                                                                                                     2.Seconds());
                                                                      anima.AutoReverse = true;
                                                                      anima.RepeatBehavior = RepeatBehavior.Forever;
                                                                      txtStatus.Background = red;
                                                                      red.BeginAnimation(SolidColorBrush.ColorProperty,
                                                                                         anima);
                                                                  }}
                                           } );
            _counter.Start();
            txtStatus.DataContext = "Time to get setup... you've got till 10:00";
        }

        private void UpdateDisplay( TimeSpan timeLeft )
        {
            string format;
            if ( timeLeft.Days > 0 )
                format = "{0:D2}:{1:D2}:{2:D2}:{3:D2}";
            else if ( timeLeft.Hours > 0 )
                format = "{1:D2}:{2:D2}:{3:D2}";
            else
                format = "{2:D2}:{3:D2}";

            txtTime.DataContext = new TimerInfo
                                      {
                                          Color = new SolidColorBrush(_currentColor),
                                          Text = string.Format(format, timeLeft.Days, timeLeft.Hours,
                                                               timeLeft.Minutes, timeLeft.Seconds)
                                      };
        }
    }

    public static class IntExt
    {
        public static TimeSpan Minutes( this int target )
        {
            return new TimeSpan( 0, target, 0 );
        }

        public static TimeSpan Seconds( this int target )
        {
            return new TimeSpan( 0, 0, target );
        }
    }

    public class TimerInfo
    {
        public string Text { get; set; }
        public SolidColorBrush Color { get; set; }
    }
}
